package com.jinzhi.jzweb.controller;


import java.util.*;
import java.util.stream.Collectors;

import com.jinzhi.jzweb.domain.ReserveUserOrderDetailsDO;
import com.jinzhi.jzweb.service.ReserveUserOrderDetailsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.ReserveUserOrderDO;
import com.jinzhi.jzweb.service.ReserveUserOrderService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 预定商品用户订单表
 * </pre>
 * <small> 2023-08-21 11:21:59 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/reserveUserOrder")
public class ReserveUserOrderController extends BaseController {
	@Autowired
	private ReserveUserOrderService reserveUserOrderService;

	@Autowired
	private ReserveUserOrderDetailsService reserveUserOrderDetailsService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:reserveUserOrder:reserveUserOrder")
	String ReserveUserOrder(){
	    return "jzweb/reserveUserOrder/reserveUserOrder";
	}

	@Log("订单详情")
	@GetMapping("/detailsReserveUserOrder/{id}")
	String detailsReserveUserOrder(@PathVariable("id") Long id, Model model) {
		EntityWrapper<ReserveUserOrderDetailsDO> entityWrapper =new EntityWrapper();
		entityWrapper.eq("order_id",id);
		List<ReserveUserOrderDetailsDO> reserveUserOrderDetailsDO =reserveUserOrderDetailsService.selectList(entityWrapper);
		model.addAttribute("reserveUserOrderDetailsDO", reserveUserOrderDetailsDO);
		model.addAttribute("id", id);
		return "jzweb/reserveUserOrderDetails/reserveUserOrderDetails";
	}


	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:reserveUserOrder:reserveUserOrder")
	public Result<Page<ReserveUserOrderDO>> list(ReserveUserOrderDO reserveUserOrderDTO){
		EntityWrapper entityWrapper =new EntityWrapper();
		if (!"".equals(reserveUserOrderDTO.getReceivingName()) && reserveUserOrderDTO.getReceivingName() !=null){
			entityWrapper.eq("receiving_name",reserveUserOrderDTO.getReceivingName());
		}
		entityWrapper.eq("order_status","支付成功");
		entityWrapper.orderBy("create_time",false);
		// 查询列表数据
		Page<ReserveUserOrderDO> page = reserveUserOrderService.selectPage(getPage(ReserveUserOrderDO.class),
					entityWrapper);
		List<ReserveUserOrderDO> list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				ReserveUserOrderDO obj = (ReserveUserOrderDO) page.getRecords().get(i);
			list.add(obj);

			EntityWrapper wrapper =new EntityWrapper();
			wrapper.eq("order_id",obj.getId());
			List<ReserveUserOrderDetailsDO> reserveUserOrderDetailsDOS = reserveUserOrderDetailsService.selectList(wrapper);

			String type = "1";//默认已发货
			EntityWrapper wrapperType =new EntityWrapper();
			wrapperType.eq("order_id",obj.getId());
			wrapperType.eq("type",type);
			List<ReserveUserOrderDetailsDO> reserveUserOrderDetailsTypeDOS = reserveUserOrderDetailsService.selectList(wrapperType);
			obj.setType(reserveUserOrderDetailsDOS.size() == reserveUserOrderDetailsTypeDOS.size()?1:0);
		}
		List<ReserveUserOrderDO> collect = list.stream().sorted(Comparator.comparing(ReserveUserOrderDO::getType)).collect(Collectors.toList());
		page.setRecords(collect);
        return Result.ok(page);
	}



	@Log("订单详情")
	@GetMapping("/selUserOrderById")
	@ResponseBody
	Result<ReserveUserOrderDO>  selUserOrderById(Long id) {
		ReserveUserOrderDO reserveUserOrderDO = reserveUserOrderService.selectById(id);
		return Result.ok(reserveUserOrderDO);
	}


	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:reserveUserOrder:add")
	String add(){
	    return "jzweb/reserveUserOrder/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:reserveUserOrder:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ReserveUserOrderDO reserveUserOrder = reserveUserOrderService.selectById(id);
		model.addAttribute("reserveUserOrder", reserveUserOrder);
	    return "jzweb/reserveUserOrder/edit";
	}
	
	@Log("添加预定商品用户订单表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:reserveUserOrder:add")
	public Result<String> save( ReserveUserOrderDO reserveUserOrder){
		reserveUserOrderService.insert(reserveUserOrder);
        return Result.ok();
	}
	
	@Log("修改预定商品用户订单表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:reserveUserOrder:edit")
	public Result<String>  update( ReserveUserOrderDO reserveUserOrder){
		boolean update = reserveUserOrderService.updateById(reserveUserOrder);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除预定商品用户订单表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:reserveUserOrder:remove")
	public Result<String>  remove( Long id){
		reserveUserOrderService.deleteById(id);
        return Result.ok();
	}

	@Log("批量删除预定商品用户订单表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:reserveUserOrder:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		reserveUserOrderService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
